<?php
/***
 * $sql="SELECT op_value FROM ".$this->ecs->table('options')." WHERE op_key='appid'";
 * $is_appid=$this->db->getOne($sql);
 */

class newsModel
{
	private $db;
	private $ecs;
	public function __construct()
	{
		$this->db = $GLOBALS['db'];
		$this->ecs = $GLOBALS['ecs'];
	}
	
	public function get_trends_info($wxid)
	{
		//社区首页广告
		$sql="SELECT ad_id,ad_name,ad_link,ad_code FROM ".$this->ecs->table("ad")." WHERE position_id =4 AND enabled=1";
		$result['ad']  = $this->db->getAll($sql);
		//置顶（精品）帖子
		$where = " p1.audit_status = 2 AND is_top = 1 ";
		$order = " ORDER BY create_time DESC LIMIT 5 ";
// 		$user_table = " (SELECT p3.user_id,p4.headimgurl FROM ".$this->ecs->table('users')." p3 LEFT JOIN `wxch_user` p4 ON p3.wxid=p4.wxid ) p2 ";
		$sql = "SELECT p1.id,p2.headimgurl,p1.like_num,p1.comment_num,p1.collect_times,p1.title FROM ".
				$this->ecs->table('news')."p1 ".
				" LEFT JOIN `wxch_user` p2  ON p1.create_id=p2.uid ".
				" WHERE $where $order";
		$top_news = $this->db->getAll($sql);
		$whereC.=" AND collecter = $wxid "; 
		$whereL.=" AND liker = $wxid "; 
		foreach($top_news as $k=>$v)
		{
			$top_news[$k]['headimgurl'] = subHeadimgurl($v['headimgurl']);
			//缩略图
			$sql = "SELECT thumb FROM ".$this->ecs->table('news_img')." WHERE news_id = ".$v['id']." LIMIT 1 ";
			$top_news[$k]['thumb'] = $this->db->getOne($sql);
			//是否点赞
			$sql = "SELECT id FROM ".$this->ecs->table('news_like')." WHERE news_id = ".$v['id']." AND like_status = 1 $whereL LIMIT 1 ";
			$id = $this->db->getOne($sql);
			if($id){
				$top_news[$k]['is_like'] = "liked";
			}else{
				$top_news[$k]['is_like'] = "";
			}
			//是否收藏
			$sql = "SELECT id FROM ".$this->ecs->table('news_collect')." WHERE news_id = ".$v['id']." AND collect_status = 1 $whereC LIMIT 1 ";
			$id = $this->db->getOne($sql);
			if($id){
				$top_news[$k]['is_collect'] = "collected";
			}else{
				$top_news[$k]['is_collect'] = "";
			}
		}
		
		$result['top_news'] = $top_news;
		
		/* 获取文章数据 */
		$sql = 'SELECT c.article_id,c.title,c.file_url,c.description,ac.cat_name,c.cat_id '.
				'FROM ' .$GLOBALS['ecs']->table('article'). ' AS c '.
				'LEFT JOIN ' .$GLOBALS['ecs']->table('article_cat'). ' AS ac ON ac.cat_id = c.cat_id '.
				'WHERE 1=1 AND is_open = 1 ORDER by c.sort_order DESC LIMIT 5';
		$top_acticle = $this->db->getAll($sql);
		$result['top_acticle'] = $top_acticle;
		return $result;
	}
	
	public function get_my_collect($wxid)
	{
		$where = " p1.collect_status = 1 AND p1.collecter = $wxid ";
		$order = " ORDER BY collect_time DESC ";
		$sql = "SELECT p2.id,p2.content,p1.collect_time,p2.like_num,p2.comment_num,p2.collect_times,p2.create_time,p1.news_id ".
				" FROM ".$this->ecs->table('news_collect')." p1 ".
				" LEFT JOIN  ".$this->ecs->table('news')." p2 ON p1.news_id = p2.id ".
				" WHERE $where $order";
		$my_collect = $this->db->getAll($sql);
		foreach($my_collect as $k=>$v)
		{
			$sql = "SELECT thumb FROM ".$this->ecs->table('news_img')." WHERE news_id = ".$v['id']." LIMIT 1 ";
			$my_collect[$k]['thumb'] = $this->db->getOne($sql);
			$my_collect[$k]['is_collect'] = "移除关注";
			//是否点赞
			$sql = "SELECT id FROM ".$this->ecs->table('news_like')." WHERE news_id = ".$v['id']." AND like_status = 1 AND liker = $wxid LIMIT 1 ";
			$id = $this->db->getOne($sql);
			if($id){
				$my_collect[$k]['is_like'] = "liked";
			}else{
				$my_collect[$k]['is_like'] = "";
			}
			
		}
		return $my_collect;
	}
	
	public function collect_new($wxid,$news_id)
	{
		$sql = "SELECT id,collect_status FROM ".$this->ecs->table('news_collect')." WHERE collecter = $wxid AND news_id = $news_id LIMIT 1 ";
		$row = $this->db->getRow($sql);
		if($row)//有值
		{
			if($row['collect_status']==1)//取消
			{
				$info['collect_status'] = 0 ;
				$result['id'] = $this->update($info, array("table"=>"news_collect","id"=>$row['id']));
				$result['codeMsg'] = "cancel"; 
				$this->add_sub_number("news","collect_times", "-1", $news_id);
				
			}else//收藏
			{
				$info['collect_status'] = 1 ;
				$info['collect_time'] = yf_time();
				$result['id'] = $this->update($info, array("table"=>"news_collect","id"=>$row['id']));
				$result['codeMsg'] = "collect";
				$this->add_sub_number("news","collect_times", "+1", $news_id);
			}
		}else//无值
		{
			$info['collect_status'] = 1;
			$info['news_id'] = $news_id;
			$info['collecter'] = $wxid;
			$info['collect_time'] = yf_time();
			$result['id'] = $this->insert($info, "news_collect");
			$result['codeMsg'] = "collect";
			$this->add_sub_number("news","collect_times", "+1", $news_id);
		}
		return $result;
	}
	public function insert($info,$table)
	{
		foreach($info as $k=>$v)
		{
			if($v!='')
			{
				$field.="$k,";
	
	
				$value.="'$v',";
					
			}
		}
		$field=substr($field,0,strlen($field)-1);
		$value=substr($value,0,strlen($value)-1);
			
		$sql="INSERT INTO ".$this->ecs->table($table)." (".$field.") VALUES(".$value.")";
	
		if(!$res=$this->db->query($sql))
		{
			return false;
		}
		else
		{
			return $this->db->insert_id();
		}
	}
	public function update($info,$cfg)
	{
		if($cfg['id'])
		{
			foreach($info as $k=>$v)
			{
				$where.=" $k='$v',";
			}
	
			$where=substr($where,0,strlen($where)-1);
	
			$sql="UPDATE ".$this->ecs->table($cfg['table'])." SET ".$where." WHERE id='".$cfg['id']."'";
			return $res=$this->db->query($sql);
		}
	}
	
	public function get_news_info($news_id,$wxid)
	{
		$sql = "SELECT p1.id,p1.content,p1.create_time,p1.like_num,p1.comment_num,p1.collect_times,p2.headimgurl,p2.nickname as name ".
				" FROM ".$this->ecs->table('news')." p1 ".
				"LEFT JOIN `wxch_user` p2  ON p1.create_id=p2.uid ".
				" WHERE p1.id = $news_id AND (p1.create_id=$wxid OR p1.audit_status<>3) LIMIT 1 ";
		$news = $this->db->getRow($sql);
		if($news)
		{
			$news['headimgurl'] = subHeadimgurl($news['headimgurl']);
			$result['news'] = $news;
			$whereC.=" AND collecter = $wxid ";
			$whereL.=" AND liker = $wxid ";
			//是否点赞
			$sql = "SELECT id FROM ".$this->ecs->table('news_like')." WHERE news_id = ".$news_id." AND like_status = 1 $whereL LIMIT 1 ";
			$id = $this->db->getOne($sql);
			if($id){
				$result['news']['is_like'] = "liked";
			}else{
				$result['news']['is_like'] = "";
			}
			//是否收藏
			$sql = "SELECT id FROM ".$this->ecs->table('news_collect')." WHERE news_id = ".$news_id." AND collect_status = 1 $whereC LIMIT 1 ";
			$id = $this->db->getOne($sql);
			if($id){
				$result['news']['is_collect'] = "collected";
			}else{
				$result['news']['is_collect'] = "";
			}
			
			//帖子下的图片
			$sql = "SELECT img,thumb FROM ".$this->ecs->table('news_img')." WHERE news_id = $news_id ";
			$result['img'] = $this->db->getAll($sql);
			//点赞群体
			$sql = "SELECT p1.id,p1.liker,p2.headimgurl FROM ".
					$this->ecs->table('news_like')." p1 LEFT JOIN `wxch_user` p2 ON p1.liker = p2.uid WHERE p1.like_status = 1 AND p1.news_id = ".$news_id." ORDER BY p1.like_time ASC ";
			$likes = $this->db->getAll($sql);
			foreach ($likes as $k=>$v){
				$likes[$k]['headimgurl'] = subHeadimgurl($v['headimgurl']);
			}
			$result['likes'] = $likes;
			//一级评论
			$sql = " SELECT p1.id,p1.comment_time,p1.comment,p2.headimgurl,p2.nickname as name,p1.commenter,p1.parent_id ".
					" FROM ".$this->ecs->table("news_comment")." p1 ".
					" LEFT JOIN `wxch_user` p2  ON p1.commenter=p2.uid ".
					" WHERE p1.news_id = $news_id AND p1.is_delete = 0 ORDER BY p1.comment_time DESC LIMIT 7 ";
			$comment = $this->db->getAll($sql);
			foreach ($comment as $k=>$v)
			{//上级评论
				$comment[$k]['headimgurl'] = subHeadimgurl($v['headimgurl']);
				
				//$comment[$k]['comment'] = json_decode($comment[$k]['comment']);
				
				if($v['parent_id']!=0)
				{
					$sql = "SELECT p1.comment,p2.nickname as name,p1.is_delete ".
							" FROM ".$this->ecs->table("news_comment")." p1 ".
							" LEFT JOIN `wxch_user` p2  ON p1.commenter=p2.uid ".
							" WHERE p1.id = ".$v['parent_id']." LIMIT 1 ";
					$comment[$k]['parentComment'] = $this->db->getRow($sql);
					if($comment[$k]['parentComment']['is_delete']==1)//已删除
					{
						$comment[$k]['parentComment']['name'] = '';
						$comment[$k]['parentComment']['comment'] = '用户已删除原评论';
						
					}
					

					//$comment[$k]['parentComment']['comment'] = json_decode($comment[$k]['parentComment']['comment']);
					
				}else{
					$comment[$k]['parentComment'] = array();
				}
			}
			$result['comment'] = $comment;
			
			return $result;
		}else{
			return $news;
		}
		
	}
	public function like_new($wxid,$news_id)
	{
		$sql = "SELECT id,like_status FROM ".$this->ecs->table('news_like')." WHERE liker = $wxid AND news_id = $news_id LIMIT 1 ";
		$row = $this->db->getRow($sql);
		if($row)//有值
		{
			if($row['like_status']==1)//取消
			{
				$info['like_status'] = 0 ;
				$result['id'] = $this->update($info, array("table"=>"news_like","id"=>$row['id']));
				$result['codeMsg'] = "cancel"; 
				$this->add_sub_number("news","like_num", "-1", $news_id);
			}else//收藏
			{
				$info['like_status'] = 1 ;
				$info['like_time'] = yf_time() ;
				$result['id'] = $this->update($info, array("table"=>"news_like","id"=>$row['id']));
				$result['codeMsg'] = "liked";
				$this->add_sub_number("news","like_num", "+1", $news_id);
			}
		}else//无值
		{
			$info['like_status'] = 1;
			$info['news_id'] = $news_id;
			$info['liker'] = $wxid;
			$info['like_time'] = yf_time() ;
			$result['id'] = $this->insert($info, "news_like");
			$result['codeMsg'] = "liked";
			$this->add_sub_number("news","like_num", "+1", $news_id);
		}
		return $result;
	}
	
	public function add_sub_number($table,$field,$number,$id)
	{
		$sql = "UPDATE ".$this->ecs->table($table). " SET $field = $field $number WHERE id = $id ";
		$this->db->query($sql);	
	}
	
	public function add_notify($id)
	{
	    
	    $sql = " SELECT user_id FROM  ".$this->ecs->table("users")." AS p1 , wxch_user AS p2 , ".$this->ecs->table("news")." AS p3 ".
	           " WHERE p3.id = '$id' AND p3.create_id = p2.uid AND p2.wxid = p1.wxid  ";
	    
	    $user_id = $this->db->getOne($sql);
	    
	    $sql = "UPDATE ".$this->ecs->table("users"). " SET notify = notify + 1 WHERE user_id = '$user_id' ";
	    $this->db->query($sql);
	}
	
	public function get_user_info($wxid)
	{
		if($wxid||$wxid==0){
			$sql = "SELECT uid as wxid ,headimgurl,nickname as name FROM `wxch_user` WHERE uid = $wxid LIMIT 1 ";
			$info = $this->db->getRow($sql);
			$info['headimgurl'] = subHeadimgurl($info['headimgurl']);
		}else{
			$info['wxid'] = 0;
		}
		return $info;
	}
	
	public function del_comment($wxid,$comment_id)
	{
		$sql = "UPDATE ".$this->ecs->table('news_comment')." SET is_delete = 1   WHERE id = $comment_id AND commenter = $wxid ";
		$result = $this->db->query($sql);
		return $result;
	}
	
	public function get_community_info($wxid)
	{
		//社区首页广告
		$sql="SELECT ad_id,ad_name,ad_link,ad_code FROM ".$this->ecs->table("ad")." WHERE position_id =5 AND enabled=1";
		$result['ad']  = $this->db->getAll($sql);
		//帖子
		$endTime = yf_time();
// 		$where = " p1.audit_status = 2 AND p1.create_time < $endTime";
		$where = " p1.audit_status !=3 AND p1.create_time < $endTime";
		
// 		if($startTime!=''){
// 			$where.=" AND p1.create_time > $startTime ";
// 		}else{//上啦刷新
// 			if($endTime==""){
				
// 			}
// 			$where.="  ";
// 		}
		$order = " ORDER BY p1.create_time DESC LIMIT 15 ";
		// 		$user_table = " (SELECT p3.user_id,p4.headimgurl FROM ".$this->ecs->table('users')." p3 LEFT JOIN `wxch_user` p4 ON p3.wxid=p4.wxid ) p2 ";
		$sql = "SELECT p1.id,p2.headimgurl,p1.like_num,p1.comment_num,p1.collect_times,p1.content,p2.nickname as name,p1.create_time,p1.create_id FROM ".
				$this->ecs->table('news')."p1 ".
				" LEFT JOIN `wxch_user` p2  ON p1.create_id=p2.uid ".
				" WHERE $where $order";
		$top_news = $this->db->getAll($sql);
		$whereC.=" AND collecter = $wxid ";
		$whereL.=" AND liker = $wxid ";
		foreach($top_news as $k=>$v)
		{
			$top_news[$k]['headimgurl'] = subHeadimgurl($v['headimgurl']);
			//缩略图
			$sql = "SELECT thumb,img FROM ".$this->ecs->table('news_img')." WHERE news_id = ".$v['id']." ";
			$top_news[$k]['thumb'] = $this->db->getAll($sql);
			//是否点赞
			$sql = "SELECT id FROM ".$this->ecs->table('news_like')." WHERE news_id = ".$v['id']." AND like_status = 1 $whereL LIMIT 1 ";
			$id = $this->db->getOne($sql);
			if($id){
				$top_news[$k]['is_like'] = "liked";
			}else{
				$top_news[$k]['is_like'] = "";
			}
				//是否收藏
			$sql = "SELECT id FROM ".$this->ecs->table('news_collect')." WHERE news_id = ".$v['id']." AND collect_status = 1 $whereC LIMIT 1 ";
			$id = $this->db->getOne($sql);
			if($id){
				$top_news[$k]['is_collect'] = "collected";
			}else{
				$top_news[$k]['is_collect'] = "";
			}
		}
		$result['news'] = $top_news;
		
		return $result;
	}
	
	
	public function refresh_community_info($wxid,$startTime='',$endTime='')
	{
		//帖子
		$where = " p1.audit_status = 2 ";
		if($startTime!=''){
			$where.=" AND p1.create_time > $startTime ";
		}else{//上啦刷新
			if($endTime==""){
				$endTime = yf_time();
			}
			$where.=" AND p1.create_time < $endTime ";
		}
		$order = " ORDER BY p1.create_time DESC LIMIT 15 ";
		// 		$user_table = " (SELECT p3.user_id,p4.headimgurl FROM ".$this->ecs->table('users')." p3 LEFT JOIN `wxch_user` p4 ON p3.wxid=p4.wxid ) p2 ";
		$sql = "SELECT p1.id,p2.headimgurl,p1.like_num,p1.comment_num,p1.collect_times,p1.content,p2.nickname as name,p1.create_time,p1.create_id FROM ".
				$this->ecs->table('news')."p1 ".
				" LEFT JOIN `wxch_user` p2  ON p1.create_id=p2.uid ".
				" WHERE $where $order";
		$top_news = $this->db->getAll($sql);
		$whereC.=" AND collecter = $wxid ";
		$whereL.=" AND liker = $wxid ";
		foreach($top_news as $k=>$v)
		{
			$top_news[$k]['headimgurl'] = subHeadimgurl($v['headimgurl']);
			//缩略图
			$sql = "SELECT thumb,img FROM ".$this->ecs->table('news_img')." WHERE news_id = ".$v['id']." ";
			$top_news[$k]['thumb'] = $this->db->getAll($sql);
			//是否点赞
			$sql = "SELECT id FROM ".$this->ecs->table('news_like')." WHERE news_id = ".$v['id']." AND like_status = 1 $whereL LIMIT 1 ";
			$id = $this->db->getOne($sql);
			if($id){
				$top_news[$k]['is_like'] = "liked";
			}else{
				$top_news[$k]['is_like'] = "";
			}
			//是否收藏
			$sql = "SELECT id FROM ".$this->ecs->table('news_collect')." WHERE news_id = ".$v['id']." AND collect_status = 1 $whereC LIMIT 1 ";
			$id = $this->db->getOne($sql);
			if($id){
				$top_news[$k]['is_collect'] = "collected";
			}else{
				$top_news[$k]['is_collect'] = "";
			}
		}
		return $top_news;
	}
	
	public function del_news($wxid,$news_id)
	{
		$sql = "DELETE FROM ".$this->ecs->table('news')."  WHERE id = $news_id AND create_id = $wxid ";
		$result = $this->db->query($sql);
		if($result){
			//清除关联字表的数据
			//收藏
			$sql = "DELETE FROM ".$this->ecs->table('news_collect')."  WHERE news_id = $news_id ";
			$resu = $this->db->query($sql);
			//评论
			$sql = "DELETE FROM ".$this->ecs->table('news_comment')."  WHERE news_id = $news_id ";
			$resu = $this->db->query($sql);
			//图片
			$sql = "DELETE FROM ".$this->ecs->table('news_img')."  WHERE news_id = $news_id ";
			$resu = $this->db->query($sql);
			//点赞
			$sql = "DELETE FROM ".$this->ecs->table('news_like')."  WHERE news_id = $news_id ";
			$resu = $this->db->query($sql);
		}
		return $result;
	}
	
	public function get_community_new_info($catId = "ALL",$search="")
	{
		//社区首页广告
		$sql="SELECT ad_id,ad_name,ad_link,ad_code FROM ".$this->ecs->table("ad")." WHERE position_id =5 AND enabled=1";
		$result['ad']  = $this->db->getAll($sql);
		//分类
		$sql = "SELECT cat_id,cat_name  FROM ".$this->ecs->table("article_cat")." WHERE show_in_nav =1 ORDER BY sort_order ASC ";
		$catList = $this->db->getAll($sql);
		
		
		
		
		foreach ($catList as $k=>$v)
		{
		    
		    
		    
			if($v['cat_id']==$catId){
				$catList[$k]['is_select'] = "selected";
			}
		}
		
		//第一列三个分类
		$catList1 = array_slice($catList,0,3);
		
		//第二列四个分类
		$catList2 = array_slice($catList,3,4);
		
		$result['articleCat'] = $catList1;
		$result['articleCa'] = $catList2;
		//教程详情
		$where = " p1.is_open=1 ";
		if($catId !="ALL")
		{
			$where.=" AND p1.cat_id = ".$catId;
			$order = " ORDER BY p1.sort_order desc,p1.add_time desc ";
		}
		else 
		{
		    $where.=" AND p1.show_in_nav = 1 ";
		    $order = " ORDER BY p1.nav_order desc,p1.add_time desc ";
		}
		if($search !="")
		{
			$where.=" AND p1.title like '%".$search."%' ";
		}
		
		$pageSize = constant("PAGE_SIZE");
		$sql="SELECT p1.article_id,p1.title,p1.file_url,p1.add_time,p1.description,p2.cat_name FROM ".$this->ecs->table("article")." p1 LEFT JOIN ".$this->ecs->table('article_cat')." p2 ON p1.cat_id=p2.cat_id WHERE $where $order LIMIT $pageSize ";
		$res = $this->db->getAll($sql);
		foreach ($res as $k=>$v){
			$res[$k]["add_time_show"] = date('Y-m-d',$v['add_time']);
		}
		$result['articles'] = $res;
		return $result;
	}
	
	public function refresh_community_new_info($catId,$search,$page=1)
	{
		//教程详情
		$where = " p1.is_open=1 ";
		if($catId !="ALL")
		{
			$where.=" AND p1.cat_id = ".$catId;
		}
		else
		{
		    $where.=" AND p1.show_in_nav = 1";
		}
		
		if($search !="")
		{
			$where.=" AND p1.title like '%".$search."%' ";
		}
		$pageSize = constant("PAGE_SIZE");
		$pageNum = ($page-1)*$pageSize;
		$order = "  ORDER BY p1.sort_order desc,p1.add_time desc LIMIT ".$pageNum.",".$pageSize;
		$sql="SELECT p1.article_id,p1.title,p1.file_url,p1.add_time,p1.description,p2.cat_name FROM ".$this->ecs->table("article")." p1 LEFT JOIN ".$this->ecs->table('article_cat')." p2 ON p1.cat_id=p2.cat_id WHERE $where $order ";
		$result = $this->db->getAll($sql);
		foreach ($result as $k=>$v){
			$result[$k]["add_time_show"] = date('Y-m-d',$v['add_time']);
		}
		return $result;
	}
	
	public function get_trends_new_info($wxid,$catId)
	{
		//社区首页广告
		$sql="SELECT ad_id,ad_name,ad_link,ad_code FROM ".$this->ecs->table("ad")." WHERE position_id =4 AND enabled=1";
		$result['ad']  = $this->db->getAll($sql);
		//分类
		$sql = "SELECT id,circle_name  FROM ".$this->ecs->table("circle")." order by sort_order ASC ";
		$newsCat = $this->db->getAll($sql);
		foreach ($newsCat as $k=>$v)
		{
			if($v['id']==$catId){
				$newsCat[$k]['active'] = "active";
			}
		}
		$result['newsCat'] = $newsCat;
		//最新帖子
		$endTime = yf_time();
		$where = " p1.audit_status <> 3 AND p1.create_time < $endTime";
		if($catId!="ALL"){
				$where.=" AND p1.circle_id = $catId ";
			}
		$order = " ORDER BY p1.create_time DESC LIMIT 5 ";
		$sql = "SELECT p1.id,p2.headimgurl,p1.like_num,p1.comment_num,p1.collect_times,p1.content,p2.nickname as name,p1.create_time,p1.create_id FROM ".
				$this->ecs->table('news')." p1 ".
				" LEFT JOIN `wxch_user` p2  ON p1.create_id=p2.uid ".
				" WHERE $where $order";
		$newest = $this->db->getAll($sql);
		$whereC.=" AND collecter = $wxid ";
		$whereL.=" AND liker = $wxid ";
		foreach($newest as $k=>$v)
		{
			$newest[$k]['headimgurl'] = subHeadimgurl($v['headimgurl']);
			//缩略图
			$sql = "SELECT thumb,img FROM ".$this->ecs->table('news_img')." WHERE news_id = ".$v['id']." ";
			$newest[$k]['thumb'] = $this->db->getAll($sql);
			//是否点赞
			$sql = "SELECT id FROM ".$this->ecs->table('news_like')." WHERE news_id = ".$v['id']." AND like_status = 1 $whereL LIMIT 1 ";
			$id = $this->db->getOne($sql);
			if($id){
				$newest[$k]['is_like'] = "liked";
			}else{
				$newest[$k]['is_like'] = "";
			}
			//是否收藏
			$sql = "SELECT id FROM ".$this->ecs->table('news_collect')." WHERE news_id = ".$v['id']." AND collect_status = 1 $whereC LIMIT 1 ";
			$id = $this->db->getOne($sql);
			if($id){
				$newest[$k]['is_collect'] = "collected";
			}else{
				$newest[$k]['is_collect'] = "";
			}
		}
		$result['newest'] = $newest;
		//精品帖子
		$where = " p1.is_top = 1 AND p1.audit_status <> 3 AND p1.create_time < $endTime";
		if($catId!="ALL"){
			$where.=" AND p1.circle_id = $catId ";
		}
		$order = " ORDER BY p1.sort_order desc , p1.create_time DESC LIMIT 15 ";
		$sql = "SELECT p1.id,p2.headimgurl,p1.like_num,p1.comment_num,p1.collect_times,p1.content,p2.nickname as name,p1.create_time,p1.create_id,p1.title FROM ".
				$this->ecs->table('news')." p1 ".
				" LEFT JOIN `wxch_user` p2  ON p1.create_id=p2.uid ".
				" WHERE $where $order";
		$news_top = $this->db->getAll($sql);
		foreach($news_top as $k=>$v)
		{
			$news_top[$k]['headimgurl'] = subHeadimgurl($v['headimgurl']);
			//缩略图
			$sql = "SELECT thumb FROM ".$this->ecs->table('news_img')." WHERE news_id = ".$v['id']." LIMIT 1 ";
			$news_top[$k]['thumb'] = $this->db->getOne($sql);
			//是否点赞
			$sql = "SELECT id FROM ".$this->ecs->table('news_like')." WHERE news_id = ".$v['id']." AND like_status = 1 $whereL LIMIT 1 ";
			$id = $this->db->getOne($sql);
			if($id){
				$news_top[$k]['is_like'] = "liked";
			}else{
				$news_top[$k]['is_like'] = "";
			}
			//是否收藏
			$sql = "SELECT id FROM ".$this->ecs->table('news_collect')." WHERE news_id = ".$v['id']." AND collect_status = 1 $whereC LIMIT 1 ";
			$id = $this->db->getOne($sql);
			if($id){
				$news_top[$k]['is_collect'] = "collected";
			}else{
				$news_top[$k]['is_collect'] = "";
			}
		}
		$result['news_top'] = $news_top;
		return $result;
	}
	
	public function refresh_trends_new_info($wxid,$startTime='',$endTime='',$reSet=0,$tabId="newest",$catId="ALL",$page = 1)
	{
// 	    echo $page;
// 	    echo $reSet;
	    $pageSize = 5;
	    	
	    $pageNum = 1;
	    
		if($reSet==1)
		{//重置
			//最新帖子
			$endTime = yf_time();
			$where = " p1.audit_status <> 3 AND p1.create_time < $endTime";
			if($catId!="ALL"){
				$where.=" AND p1.circle_id = $catId ";
			}
			$order = " ORDER BY p1.create_time DESC LIMIT 15 ";
			$sql = "SELECT p1.id,p2.headimgurl,p1.like_num,p1.comment_num,p1.collect_times,p1.content,p2.nickname as name,p1.create_time,p1.create_id FROM ".
					$this->ecs->table('news')." p1 ".
					" LEFT JOIN `wxch_user` p2  ON p1.create_id=p2.uid ".
					" WHERE $where $order";
			$newest = $this->db->getAll($sql);
			$whereC.=" AND collecter = $wxid ";
			$whereL.=" AND liker = $wxid ";
			foreach($newest as $k=>$v)
			{
				$newest[$k]['headimgurl'] = subHeadimgurl($v['headimgurl']);
				//缩略图
				$sql = "SELECT thumb,img FROM ".$this->ecs->table('news_img')." WHERE news_id = ".$v['id']." ";
				$newest[$k]['thumb'] = $this->db->getAll($sql);
				//是否点赞
				$sql = "SELECT id FROM ".$this->ecs->table('news_like')." WHERE news_id = ".$v['id']." AND like_status = 1 $whereL LIMIT 1 ";
				$id = $this->db->getOne($sql);
				if($id){
					$newest[$k]['is_like'] = "liked";
				}else{
					$newest[$k]['is_like'] = "";
				}
				//是否收藏
				$sql = "SELECT id FROM ".$this->ecs->table('news_collect')." WHERE news_id = ".$v['id']." AND collect_status = 1 $whereC LIMIT 1 ";
				$id = $this->db->getOne($sql);
				if($id){
					$newest[$k]['is_collect'] = "collected";
				}else{
					$newest[$k]['is_collect'] = "";
				}
			}
			$result['newest'] = $newest;
			//精品帖子
			$where = " p1.is_top = 1 AND p1.audit_status <> 3 AND p1.create_time < $endTime";
			if($catId!="ALL"){
				$where.=" AND p1.circle_id = $catId ";
			}
			$order = " ORDER BY p1.create_time DESC LIMIT 15 ";
			$sql = "SELECT p1.id,p2.headimgurl,p1.like_num,p1.comment_num,p1.collect_times,p1.content,p2.nickname as name,p1.create_time,p1.create_id,p1.title FROM ".
					$this->ecs->table('news')." p1 ".
					" LEFT JOIN `wxch_user` p2  ON p1.create_id=p2.uid ".
					" WHERE $where $order";
			$news_top = $this->db->getAll($sql);
			foreach($news_top as $k=>$v)
			{
				$news_top[$k]['headimgurl'] = subHeadimgurl($v['headimgurl']);
				//缩略图
				$sql = "SELECT thumb FROM ".$this->ecs->table('news_img')." WHERE news_id = ".$v['id']." LIMIT 1 ";
				$news_top[$k]['thumb'] = $this->db->getOne($sql);
				//是否点赞
				$sql = "SELECT id FROM ".$this->ecs->table('news_like')." WHERE news_id = ".$v['id']." AND like_status = 1 $whereL LIMIT 1 ";
				$id = $this->db->getOne($sql);
				if($id){
					$news_top[$k]['is_like'] = "liked";
				}else{
					$news_top[$k]['is_like'] = "";
				}
				//是否收藏
				$sql = "SELECT id FROM ".$this->ecs->table('news_collect')." WHERE news_id = ".$v['id']." AND collect_status = 1 $whereC LIMIT 1 ";
				$id = $this->db->getOne($sql);
				if($id){
					$news_top[$k]['is_collect'] = "collected";
				}else{
					$news_top[$k]['is_collect'] = "";
				}
			}
			$result['news_top'] = $news_top;
			return $result;
		}else{
		    
// 		    echo "aaa";
			//帖子
			if($tabId=="newest")
			{
				$where = " p1.audit_status <> 3 ";
				
			}elseif($tabId=="newstop")
			{
				$where = " p1.is_top = 1 AND p1.audit_status <> 3 ";
			}
			if($startTime!=''){
				$where.=" AND p1.create_time > $startTime ";
			}else{//上啦刷新
				if($endTime==""){
					$endTime = yf_time();
				}
				$where.=" AND p1.create_time < $endTime ";
			}
			if($catId!="ALL"){
				$where.=" AND circle_id = $catId ";
			}
			
			
			
// 			echo 22;
// 			exit;
			$order = " ORDER BY p1.create_time DESC LIMIT ".$pageNum.",".$pageSize;;
			$sql = "SELECT p1.id,p2.headimgurl,p1.like_num,p1.comment_num,p1.collect_times,p1.content,p2.nickname as name,p1.create_time,p1.create_id FROM ".
					$this->ecs->table('news')." p1 ".
					" LEFT JOIN `wxch_user` p2  ON p1.create_id=p2.uid ".
					" WHERE $where $order";
			
// 			echo $sql;
			$result['sql'] = $sql;
			
			$top_news = $this->db->getAll($sql);
			$whereC.=" AND collecter = $wxid ";
			$whereL.=" AND liker = $wxid ";
			foreach($top_news as $k=>$v)
			{
				$top_news[$k]['headimgurl'] = subHeadimgurl($v['headimgurl']);
				//缩略图
				$sql = "SELECT thumb,img FROM ".$this->ecs->table('news_img')." WHERE news_id = ".$v['id']." ";
				$top_news[$k]['thumb'] = $this->db->getAll($sql);
				//是否点赞
				$sql = "SELECT id FROM ".$this->ecs->table('news_like')." WHERE news_id = ".$v['id']." AND like_status = 1 $whereL LIMIT 1 ";
				$id = $this->db->getOne($sql);
				if($id){
					$top_news[$k]['is_like'] = "liked";
				}else{
					$top_news[$k]['is_like'] = "";
				}
				//是否收藏
				$sql = "SELECT id FROM ".$this->ecs->table('news_collect')." WHERE news_id = ".$v['id']." AND collect_status = 1 $whereC LIMIT 1 ";
				$id = $this->db->getOne($sql);
				if($id){
					$top_news[$k]['is_collect'] = "collected";
				}else{
					$top_news[$k]['is_collect'] = "";
				}
				
				
// 				$top_news[$k]['sql'] = $sql;
			}
		}
		$result['newest'] = $top_news;
		
		return $result;
	}
}